package com.qdsgjsfk.vision.ui.dialog;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.qdsgjsfk.vision.R;
import com.qdsgjsfk.vision.base.BaseDialogFragment;
import com.qdsgjsfk.vision.eventbus.SocketInput;
import com.qdsgjsfk.vision.model.Msg;
import com.qdsgjsfk.vision.util.Constant;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.DecimalFormat;
import java.util.Objects;

/**
 * 自查模式弹框
 * Created by lbj on 2021/4/28 9:11
 */
public class VisionSelfDialog extends BaseDialogFragment {

    public static VisionSelfDialog newInstance(float message, boolean isWearGlasses) {
        Bundle args = new Bundle();
        args.putFloat("message", message);
        args.putBoolean("isWearGlasses", isWearGlasses);
//        args.putInt("operateType", operateType);
        VisionSelfDialog fragment = new VisionSelfDialog();
        fragment.setArguments(args);
        return fragment;
    }

    private Dialog mDialog;
    private OnItemClickListener mListener;
    TextView btn_miss, btn_clear, btn_ok, btn_finish;
    ImageView btn_cancel;
    ImageView e1, e2, e3, e4;
    DecimalFormat decimalFormat;
    //是否进行戴镜检查
    private Boolean isWearGlasses;
//    private int cmd = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        isWearGlasses = getArguments().getBoolean("isWearGlasses", true);
        View view = inflater.inflate(R.layout.dialog_vision_self, container);
        btn_miss = view.findViewById(R.id.btn_miss);
        btn_finish = view.findViewById(R.id.btn_finish);
        btn_ok = view.findViewById(R.id.btn_ok);
        btn_cancel = view.findViewById(R.id.btn_cancel);
        btn_clear = view.findViewById(R.id.btn_clear);

        decimalFormat = new DecimalFormat("0.0");
        e1 = view.findViewById(R.id.e1);
        e2 = view.findViewById(R.id.e2);
        e3 = view.findViewById(R.id.e3);
        e4 = view.findViewById(R.id.e4);
        e1.post(() -> rotationImage(e1, 270));
        e2.post(() -> rotationImage(e2, 180));
        e4.post(() -> rotationImage(e4, 90));

        e1.setOnClickListener(v -> calculateSight(270));
        e2.setOnClickListener(v -> calculateSight(180));
        e3.setOnClickListener(v -> calculateSight(0));
        e4.setOnClickListener(v -> calculateSight(90));
        btn_miss.setOnClickListener(v -> EventBus.getDefault().postSticky(new SocketInput(new Msg.Builder().code(Constant.CODE_MISS).value("").build().toString())));

        btn_cancel.setOnClickListener(v -> {
            EventBus.getDefault().postSticky(new SocketInput(new Msg.Builder().code(Constant.CODE_QUIT).value("").build().toString()));
            dismiss();
        });

        btn_finish.setOnClickListener(v -> {
            EventBus.getDefault().postSticky(new SocketInput(new Msg.Builder().code(Constant.CODE_FINISH).chartType(1).build().toString()));
            dismiss();
        });

        btn_ok.setOnClickListener(v -> {
            btn_finish.setVisibility(View.GONE);
            btn_miss.setVisibility(View.VISIBLE);
            btn_ok.setText("OK");
            EventBus.getDefault().postSticky(new SocketInput(new Msg.Builder().code(Constant.CODE_OK).chartType(1).build().toString()));
        });
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void calculateSight(int angle) {
        EventBus.getDefault().postSticky(new SocketInput(new Msg.Builder().code(Constant.CODE_STUDENT_OPERATE).value(angle + "").chartType(1).build().toString()));
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Msg event) {
        if (event.code == 98) {
            if (isWearGlasses) {
                btn_finish.setVisibility(View.VISIBLE);
                btn_ok.setText("戴镜检查");
                btn_miss.setVisibility(View.GONE);
            }
//            cmd++;
        } else if (event.code == Constant.CODE_QUIT || event.code == 99) {
            dismiss();
        }
    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(false);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Objects.requireNonNull(getDialog().getWindow()).requestFeature(Window.FEATURE_NO_TITLE);
        super.onActivityCreated(savedInstanceState);
        getDialog().setCanceledOnTouchOutside(false);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }

    public interface OnItemClickListener {
        void onConfirm(String vision);

    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }

    private void rotationImage(ImageView e, int rotation) {
        e.setPivotX(e.getWidth() >> 1);
        e.setPivotY(e.getHeight() >> 1);//支点在图片中心
        e.setRotation(rotation);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
